const sql = require('mssql');
const baseFn = require('../../public/base.js');

//  获取所有商品信息
const getLogInfo = async (ctx, next) => {
    try {
        const { index, pageSize, searchList } = ctx.request.body;
        const { times, CZR } = searchList;
        let totalSql = ` SELECT COUNT(l.BM) AS total FROM log l LEFT JOIN BM_YG YG ON l.BM = YG.BM WHERE 1 = 1 `
        let selectSql = ` SELECT * FROM  ( SELECT ROW_NUMBER() OVER(ORDER BY l.CZRQ) as px,YG.MC,l.CZRQ,l.CZNR FROM log l LEFT JOIN BM_YG YG ON l.BM = YG.BM WHERE 1 = 1  `;
        let actionSqlList = [];
        if (times[0]) {
            actionSqlList.push(` AND l.CZRQ >= '${times[0]} 00:00:00' AND l.CZRQ <= '${times[1]} 23:59:59' `);
        }

        if (CZR) {
            actionSqlList.push(` AND YG.MC LIKE '%${CZR}%'  `);
        }


        totalSql += actionSqlList.join(' ');
        selectSql += ` ${actionSqlList.join(' ')} ) t1 WHERE T1.px between ${(index - 1) * pageSize + 1} and ${index * pageSize} `;
        let totalResult = await sql.query(totalSql);
        let selectResult = await sql.query(selectSql);
        ctx.body = { code: 200, message: '获取数据成功', data: { total: totalResult.recordset[0].total, result: selectResult.recordset } }

    } catch (err) {
        ctx.body = { code: 500, message: '获取数据成功', err }

    }
}



module.exports = {
    'POST /getLogInfo': getLogInfo
}
